<template>
  <view class="container">
    <template v-if="student">
      <view class="student-card">
        <image 
          :src="'/static' + student.icon"
          mode="aspectFill"
          class="student-photo"
        />
        <view class="student-info">
          <text class="student-name">{{ student.username }}</text>
          <text class="student-id">学号：{{ student.id }}</text>
          <text class="student-phone">电话：{{ student.telephone }}</text>
        </view>
      </view>
      <!-- 评论列表 -->
      <view class="comments-section">
        <text class="comments-title">评论列表</text>
        <view v-for="(comment, index) in comments" :key="index" class="comment-item">
          <text>{{ comment.comment }}</text>
        </view>
        <view v-if="comments.length === 0" class="no-comments">暂无评论</view>
      </view>
    </template>
    <view v-else class="no-data">未找到学生信息</view>

    <!-- 评论输入框 -->
    <view class="comment-input">
      <input v-model="message" placeholder="输入评论..." />
      <button @click="postMessage">发表</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      student: null,
      comments: [],
      message: '',
      page: 1, // 当前页码
      pageSize: 5 // 每页评论数量
    }
  },
  
  onLoad(options) {
    this.getStudentData(options.id);
    this.getComments(); // 获取评论
  },
  
  methods: {  
    getStudentData(id) {
      const pages = getCurrentPages();
      const prevPage = pages[pages.length - 2];
      
      if (prevPage && prevPage.$vm.list) {
        const studentList = prevPage.$vm.list;
        this.student = studentList.find(student => String(student.id) === String(id));
      }
    },
    getComments() {
      uni.request({
        url: 'http://localhost:8090/foodapp/comments/list',
        method: 'POST',
        data: {
          page: this.page,
          pageSize: this.pageSize
        },
        success: res => {
          if (res.data.success) {
            this.comments = res.data.data; // 直接赋值所有评论
          } else {
            uni.showToast({
              title: '获取评论失败',
              icon: 'error'
            });
          }
        },
        fail: () => {
          uni.showToast({
            title: '获取评论失败',
            icon: 'error'
          });
        }
      });
    },
    postMessage() {
      if (this.message.trim()) {
        const data = {
          comment: this.message,
          foodId: 1, // 假设这里用固定的 foodId
          postDate: new Date().toISOString().substring(0, 10),
          userId: uni.getStorageSync('userInfo').id // 假设用户信息存储在 localStorage 中
        };
        uni.request({
          url: 'http://localhost:8090/foodapp/comments/add',
          method: 'POST',
          data: data,
          success: res => {
            if (res.data.success) {
              this.comments.push({ comment: this.message }); // 添加到本地评论列表
              this.message = ''; 
              uni.showToast({
                title: '发表成功'
              });
            } else {
              uni.showToast({
                title: '发表失败',
                icon: 'error'
              });
            }
          },
          fail: () => {
            uni.showToast({
              title: '发表失败',
              icon: 'error'
            });
          }
        });
      } else {
        uni.showToast({
          title: '评论内容不能为空',
          icon: 'error'
        });
      }
    },
    onPullDownRefresh() {
      this.page = 1; // 重置页码
      this.getComments(); // 重新获取评论
      uni.stopPullDownRefresh(); // 停止下拉刷新
    },
    onReachBottom() {
      uni.showLoading(); // 显示加载中
      this.page++; // 增加页码
      this.getComments(); // 获取更多评论
      setTimeout(() => {
        uni.hideLoading(); // 隐藏加载中
      }, 1000); // 1秒后隐藏加载中
    }
  }
}
</script>

<style>
.container {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: space-between; /* 确保输入框在底部 */
}
.student-card {
  background-color: #ffffff;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}
.student-photo {
  width: 100%;
  height: 200px;
  object-fit: cover;
}
.student-info {
  padding: 20px;
}
.student-name {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  margin-bottom: 10px;
  display: block;
}
.student-id, .student-phone {
  font-size: 16px;
  color: #666;
  margin-bottom: 5px;
  display: block;
}
.comments-section {
  margin-top: 20px;
}
.comments-title {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 10px;
}
.comment-item {
  padding: 5px 0;
  border-bottom: 1px solid #f0f0f0;
}
.no-comments {
  text-align: center;
  color: #999;
  font-size: 16px;
}
.comment-input {
  display: flex;
  margin-top: 10px;
}
.comment-input input {
  flex: 1;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 5px;
}
.comment-input button {
  margin-left: 10px;
  background-color: #0eabff;
  color: white;
  border: none;
  border-radius: 5px;
  padding: 10px 15px;
}
.no-data {
  text-align: center;
  color: #999;
  font-size: 18px;
  margin-top: 50px;
}
</style>
